######################### DYNAMIC REGRESSION ANALYSIS ############################
##################################################################################

#Name of code file: dynamic_regression_analysis.R
#Purpose: Regression Models, Tables, and Coefficient Plots 
#Data In: dynamic_network_tolerance_processed_data.csv
#Data Out: Figure 5; Tables: B8-B10
##################################################################################

#Load packages
library(readr)
library(coefplot)
library(apsrtable)
library(MASS)
library(AER)
library(xtable)
library(lm.beta)

#Set Working Directory
setwd("~/Dropbox/egypt_tolerance_wp_replication/")

#Read in Data
time_data<-read_csv("data/dynamic_network_tolerance_processed_data.csv")


###########
#TABLE B8 #
###########

quasipoisson_intol_change_model <- glm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15 +time_data$log_total_friends +time_data$log_elite +time_data$log_time + time_data$islamist +time_data$rel_count_change_6m, family=quasipoisson())
summary(quasipoisson_intol_change_model) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_intol_change_model)))
xtable(exp_quasi)

quasipoisson_intol_change_model_no_cont <- glm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15, family=quasipoisson())
summary(quasipoisson_intol_change_model_no_cont) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_intol_change_model_no_cont)))
xtable(exp_quasi)

quasipoisson_intol_change_model_moderates <- glm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_mod_may15 +time_data$log_total_friends +time_data$log_elite +time_data$log_time + time_data$islamist +time_data$rel_count_change_6m, family=quasipoisson())
summary(quasipoisson_intol_change_model_moderates) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_intol_change_model_moderates)))
xtable(exp_quasi)

quasipoisson_intol_change_model_moderates_no_cont <- glm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_mod_may15, family=quasipoisson())
summary(quasipoisson_intol_change_model_moderates_no_cont) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_intol_change_model_moderates_no_cont)))
xtable(exp_quasi)

#Make Table
apsrtable(quasipoisson_intol_change_model_no_cont, quasipoisson_intol_change_model, quasipoisson_intol_change_model_moderates_no_cont, quasipoisson_intol_change_model_moderates, digits=4, lev=.1, stars="default")

###########
#TABLE B9 #
###########

intol_change_model <- lm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15 +time_data$log_total_friends +time_data$log_elite +time_data$log_time + time_data$islamist +time_data$rel_count_change_6m)
summary(intol_change_model) 

#Try Without Controls

intol_change_model_no_cont <- lm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15)
summary(intol_change_model_no_cont) 

#With Moderates 

intol_change_model_moderates <- lm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_mod_may15+ time_data$log_total_friends +time_data$log_elite+  time_data$log_time + time_data$islamist )
summary(intol_change_model_moderates) 

intol_change_model_no_cont_moderates <- lm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_mod_may15)
summary(intol_change_model_no_cont_moderates) 

#Make Table
apsrtable(intol_change_model_no_cont, intol_change_model, intol_change_model_no_cont_moderates, intol_change_model_moderates, digits=4, lev=.1, stars="default")

##########
#FIGURE 5#
##########

new_names<-c("time_data$elite_diversity_may15" = "Elite Diversity", "time_data$nonelite_diversity_mod_may15" = "Non-Elite Diversity \n (w/ Moderates)", "time_data$nonelite_diversity_may15"="Non-Elite Diversity\n (w/o Moderates)", "data$log_total_friends" = "Log Non-Elite Friends", "data$log_elite" ="Log Elite Friends", "data$log_relevant_tweets"="Log Relevant Tweets", "data$log_time"=" Log Days on Twitter", "data$log_status_count"="Log Total Tweets", "data$secular"="Secular", "data$islamist"="Islamist", "data$moderate"="Moderate")
pdf(file="plots/figure6.pdf", width = 11, height = 7)
plot<-multiplot(quasipoisson_intol_change_model, quasipoisson_intol_change_model_moderates, innerCI=1.645, outerCI=1.96,  sort="alphabetical",single=FALSE, coefficients=c("time_data$elite_diversity_may15","time_data$nonelite_diversity_mod_may15", "time_data$nonelite_diversity_may15"), plot=TRUE, intercept=FALSE, ylab="", xlab="", newNames=new_names, decreasing=FALSE, pointSize=5, color="red", title="", names=c("Excluding Moderates", "Including Moderates"),legend.position="bottom", ncol=2) 
plot+ theme_bw(base_size=22)+ theme(legend.position="none")
dev.off()

###########
#TABLE B10#
###########

time_data$diverse_elite_network<-factor(with(time_data, ifelse ((elite_diversity_may15>=.5), 1 ,0)))

time_data$diverse_non_elite_network<-factor(with(time_data, ifelse ((nonelite_diversity_may15>.5), 1 ,0)))

#Fixed Effect Models
intol_change_model3 <- lm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15 +time_data$log_total_friends +time_data$log_elite +time_data$log_time + time_data$islamist +time_data$rel_count_change_6m)

intol_change_model4<- lm(time_data$intolerant_may16 ~ time_data$intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15 +time_data$elite_diversity_total_change + time_data$nonelite_diversity_total_change + time_data$log_elite + time_data$log_total_friends+ time_data$islamist + time_data$log_time +time_data$rel_count_change_6m + time_data$diverse_elite_network + time_data$diverse_non_elite_network)

prop_intol_change_model3 <- lm(time_data$prop_intolerant_may16 ~ time_data$prop_intolerant_may15_6m +time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15 + time_data$log_total_friends +time_data$log_elite+  time_data$log_time + time_data$islamist+ time_data$diverse_elite_network + time_data$diverse_non_elite_network )

prop_intol_change_model4<- lm(time_data$prop_intolerant_may16 ~ time_data$prop_intolerant_may15_6m +  time_data$elite_diversity_may15 + time_data$nonelite_diversity_may15 +time_data$elite_diversity_total_change + time_data$nonelite_diversity_total_change + time_data$log_elite + time_data$log_total_friends+ time_data$islamist + time_data$log_time + time_data$diverse_elite_network + time_data$diverse_non_elite_network)

#Make Table
apsrtable(intol_change_model3, intol_change_model4, prop_intol_change_model3, prop_intol_change_model4, stars="default", lev=.1, digits=4)

















